package com.cll.pmis.common.security;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Set;

/**
 * @author chenliangliang
 * @date 2018/6/19
 */
@Data
public class SysUser {

    private Integer id;
    private String username;
    private String role;
    private Set<String> perms;


    public boolean hasAllPerms(String... perms) {
        if (perms==null||perms.length==0){
            return true;
        }
        for (String p : perms) {
            if (!this.perms.contains(p)) {
                return false;
            }
        }
        return true;
    }

    public boolean inPerms(String... perms) {
        if (perms==null||perms.length==0){
            return true;
        }
        for (String p : perms) {
            if (this.perms.contains(p)) {
                return true;
            }
        }
        return false;
    }

    public boolean noPerms(String... perms) {
        if (perms==null||perms.length==0){
            return true;
        }
        for (String p : perms) {
            if (this.perms.contains(p)) {
                return false;
            }
        }
        return true;
    }

    public boolean hasRole(String ... role) {
        if (role==null||role.length==0){
            return true;
        }
        if (role.length>1){
            return false;
        }
        return StringUtils.equalsIgnoreCase(this.role, role[0]);
    }

    public boolean inRoles(String ... roles){
        if (roles==null||roles.length==0){
            return true;
        }

        for (String r:roles){
            if (StringUtils.equalsIgnoreCase(this.role,r)){
                return true;
            }
        }
        return false;
    }

    public boolean noRoles(String ... roles){
        if (roles==null||roles.length==0){
            return true;
        }

        for (String r:roles){
            if (StringUtils.equalsIgnoreCase(this.role,r)){
                return false;
            }
        }
        return true;
    }


}
